package com.stellar.auth.domain.authUserRole.impl;

import com.stellar.auth.basic.entity.AuthUserRole;
import com.stellar.auth.basic.service.AuthUserRoleService;
import com.stellar.auth.controller.admin.authUserRole.vo.AuthUserRoleRequestPageVO;
import com.stellar.auth.controller.admin.authUserRole.vo.AuthUserRoleRequestVO;
import com.stellar.auth.controller.admin.authUserRole.vo.AuthUserRoleResponseVO;
import com.stellar.auth.convert.AuthUserRoleConvert;
import com.stellar.auth.domain.authUserRole.AuthUserRoleDomainService;

import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.enums.IsDeletedFlagEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户角色关联表领域服务实现
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-05-16 18:07:46
 */
@Service
public class AuthUserRoleDomainServiceImpl implements AuthUserRoleDomainService {

    @Resource
    private AuthUserRoleService authUserRoleService;

    @Override
    public PageResult<AuthUserRoleResponseVO> page(AuthUserRoleRequestPageVO requestPageVO) {
        PageResult<AuthUserRoleResponseVO> pageResult = new PageResult<>();
        AuthUserRole queryEntity = AuthUserRoleConvert.INSTANCE.convert(requestPageVO);
        queryEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());

        List<AuthUserRole> entities = authUserRoleService.page(queryEntity,
                requestPageVO.getStart(), requestPageVO.getPageSize());

        if (entities.isEmpty()) {
            return pageResult;
        }

        Long count = authUserRoleService.count(queryEntity);
        List<AuthUserRoleResponseVO> vos = AuthUserRoleConvert.INSTANCE.convertEntityToRespList(entities);

        pageResult.setResult(vos);
        pageResult.setTotal(count.intValue());
        return pageResult;
    }

    @Override
    public Boolean update(AuthUserRoleRequestVO requestVO) {
        AuthUserRole entity = AuthUserRoleConvert.INSTANCE.convert(requestVO);
        return authUserRoleService.update(entity) != 0;
    }

    @Override
    public Boolean delete(Long id) {
        AuthUserRole entity = new AuthUserRole();
        entity.setId(id);
        entity.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
        return authUserRoleService.update(entity) != 0;
    }

    @Override
    public Boolean insert(AuthUserRoleRequestVO requestVO) {
        AuthUserRole entity = AuthUserRoleConvert.INSTANCE.convert(requestVO);
        return authUserRoleService.insert(entity) != 0;
    }

    @Override
    public AuthUserRoleResponseVO get(Long id) {
        AuthUserRole entity = authUserRoleService.queryById(id);
        return AuthUserRoleConvert.INSTANCE.convert(entity);
    }
}
